﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;

/// <summary>
/// Summary description for bzAdminPageBase
/// </summary>
public abstract class bzAdminPageBase:System.Web.UI.Page
{

    //----------------------------------------------------------
    private DataRow _SiteOption = null;
    public DataRow SiteOption { get { return _SiteOption; } }

    private DataRow _AdminLang = null;
    public DataRow AdminLang { get { return _AdminLang; } }

    private string _Navigator = "";
    public string Navigator { get { return _Navigator; } set { _Navigator = value; } }

    private string _SearchString = "";
    public string SearchString { get { return _SearchString; } set { _SearchString = value; } }

    private string _Act = "";
    public string Act { get { return _Act; } }

    private string _CurrentLang = "VN";
    public string CurrentLang { get { return _CurrentLang; } }



    //---------Check permission
    private bool _CheckPermission = true;
    public bool CheckPermission { set { _CheckPermission = value; } }

    public bool AllowView { get { return this.check(bzCheckAdminUser.View); } }
    public bool AllowAdd { get { return this.check(bzCheckAdminUser.Type); } }
    public bool AllowEdit { get { return this.check(bzCheckAdminUser.Edit); } }
    public bool AllowDelete { get { return this.check(bzCheckAdminUser.Delete); } }
    private bool check(int Per)
    {
        return bzCheckAdminUser.checkUserPermission(Session["AdminUser"].ToString(), Session["AdminLevel"].ToString(), Session["IP"].ToString(), Session["LoginID"].ToString(), Session["GroupUser"].ToString(), Session["Page"].ToString(), Per);

    }
    //------------------------------

    public void initSiteLang(string sSiteCate, string sSiteNote, string sLangNote)
    {

        if (!Request.Url.AbsoluteUri.Contains(Session["Page"].ToString()))
        {
            Session["Page"] = Request.Url.AbsoluteUri.Split('?')[0];
        }
        Session["Lang"] = bzAdminLang.checkLang(string.IsNullOrEmpty(Request.QueryString["Lang"]) ? Session["Lang"].ToString() : Request.QueryString["Lang"]);
        _Act = string.IsNullOrEmpty(Request.QueryString["Act"]) ? "" : Request.QueryString["Act"];

        _CurrentLang = Session["Lang"].ToString().ToUpper().Trim();
        _SiteOption = new bzSiteOption(sSiteCate, sSiteNote).SiteOption;
        _AdminLang = new bzAdminLang(Session["Lang"].ToString(), sLangNote).AdminLang;
        if (this._CheckPermission && !this.AllowView)
        {
            Response.Redirect(bzStringClass.AlertPermission);
        }
        this.loadAction();
    }
    abstract protected void loadAction();




    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
    }

    //----------------Pagination


    private int _CurrentPage = 0;
    public int CurrentPage { get { return _CurrentPage; } }

    private string _Pagination = "";
    public string Pagination { get { return _Pagination; } }

    public void loadPagination(long total, int limit, string sURL)
    {
        _CurrentPage = Convert.ToInt32(string.IsNullOrEmpty(Request.QueryString["Page"]) ? "1" : this.IsPostBack ? "1" : Request.QueryString["Page"]);

        bzPagination pg = new bzPagination();
        pg.Limit = Convert.ToInt32(limit);
        pg.PageNumber = _CurrentPage;
        pg.Total = total;
        pg.Page = this.AdminLang["PageString"].ToString();
        pg.Next = this.AdminLang["PageNext"].ToString();
        pg.Last = this.AdminLang["PageLast"].ToString();
        pg.Previous = this.AdminLang["PagePrevious"].ToString();
        pg.First = this.AdminLang["PageFirst"].ToString();
        pg.ItemShowNumber = Convert.ToInt32(this.SiteOption["AdminPageLimit"]);
        pg.URL = sURL;
        _Pagination = pg.getStringPagination();
    }
    public void loadPaginationAjax(long total, int limit, string sURL, int ajaxCase)
    {
        _CurrentPage = Convert.ToInt32(string.IsNullOrEmpty(Request.QueryString["Page"]) ? "1" : this.IsPostBack ? "1" : Request.QueryString["Page"]);

        bzPagination pg = new bzPagination();
        pg.Limit = Convert.ToInt32(limit);
        pg.PageNumber = _CurrentPage;
        pg.Total = total;
        pg.Page = this.AdminLang["PageString"].ToString();
        pg.Next = this.AdminLang["PageNext"].ToString();
        pg.Last = this.AdminLang["PageLast"].ToString();
        pg.Previous = this.AdminLang["PagePrevious"].ToString();
        pg.First = this.AdminLang["PageFirst"].ToString();
        pg.ItemShowNumber = Convert.ToInt32(this.SiteOption["AdminPageLimit"]);
        pg.URL = sURL;
        _Pagination = pg.getStringPaginationAjax(ajaxCase);
    }
}
